package com.xbongbong.paas.service.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.service.CallCenterStatementService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.saas.constant.callcenter.CallCenterConstant;
import com.xbongbong.saas.domain.entity.CallCenterStatementEntity;
import com.xbongbong.saas.model.CallCenterOrganizationModel;
import com.xbongbong.saas.model.CallCenterStatementModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author youli.chen
 * @version v1.0
 * @description: TODO
 * @date 2019/2/28 9:35
 * @since v1.0
 */
@Service("callCenterStatementService")
public class CallCenterStatementServiceImpl implements CallCenterStatementService {

    @Resource
    private CallCenterStatementModel callCenterStatementModel;
    @Resource
    private CallCenterOrganizationModel callCenterOrganizationModel;

    /**
     * 获取当前月的消费
     *
     * @param corpid     公司id
     * @param moduleType 微应用平台
     * @return 对账结果（取绝对值）
     * @throws XbbException
     */
    @Override
    public BigDecimal checkCurrentMonthCost(String corpid, Integer moduleType) throws XbbException {
        return checkRangeAmount(corpid, moduleType, CallCenterConstant.STATEMENT_TYPE_CONSUME,
                DateUtil.getFirstDateOfThisMonth(), DateUtil.getInt());
    }

    /**
     * 消费
     *
     * @param corpid        公司id
     * @param moduleType    微应用平台
     * @param amount        充值金额
     * @param agentNameList 坐席列表
     * @param memo          备注
     * @return 消费成功与否
     * @throws XbbException
     */
    @Override
    public boolean consume(String corpid, Integer moduleType, BigDecimal amount, List<String> agentNameList, String memo) throws XbbException {
        StringBuilder temp = new StringBuilder();
        if (StringUtil.isNotEmpty(memo)) {
            temp.append(memo).append("\n");
        }
        temp.append(I18nMessageUtil.getMessage(CommonConstant.FOR));
        for (int i = 0; i < agentNameList.size(); i++) {
            temp.append(agentNameList.get(i));
            if (i == 3) {
                if (agentNameList.size() > 4) {
                    temp.append(I18nMessageUtil.getMessage(CommonConstant.ETC));
                    break;
                }
            } else if (i != (agentNameList.size() - 1)) {
                // 不是最后一个加"、"分隔
                temp.append("、");
            }
        }
        temp.append(I18nMessageUtil.getMessage(I18nStringConstant.AUTOMATIC_AGENT_DEDUCTION) + "。(").append(DateUtil.getString(DateUtil.SDF)).append(I18nMessageUtil.getMessage(I18nStringConstant.AUTOMATIC_PAYMENT) + ")");

        amount = new BigDecimal(-1).multiply(amount.abs());
        Integer paidAgentNum = agentNameList.size();
        CallCenterStatementEntity statement = new CallCenterStatementEntity(corpid, moduleType, amount, agentNameList,
                paidAgentNum, CallCenterConstant.STATEMENT_TYPE_CONSUME, temp.toString());
        return callCenterStatementModel.insert(statement) > 0;
    }

    /**
     * 对账得到余额
     *
     * @param corpid     公司id
     * @param moduleType 微应用平台
     * @return 对账余额
     * @throws XbbException
     */
    @Override
    public BigDecimal checkBillBalance(String corpid, Integer moduleType) throws XbbException {
        BigDecimal balance = callCenterStatementModel.checkBillBalance(corpid, moduleType);
        if (balance == null) {
            balance = new BigDecimal(0);
        }
        callCenterOrganizationModel.updateBalance(corpid, moduleType, balance);
        return balance;
    }

    /**
     * 获取制定时间的账单
     *
     * @param corpid       公司id
     * @param moduleType   微应用平台
     * @param type         对账单类型，参考CallCenterConstant
     * @param startAddTime 开始时间
     * @param endAddTime   结束时间
     * @return 对账金额，取绝对值
     */
    private BigDecimal checkRangeAmount(String corpid, Integer moduleType, Integer type, Integer startAddTime, Integer endAddTime) {
        BigDecimal cost = callCenterStatementModel.checkRangeAmount(corpid, moduleType, type, startAddTime, endAddTime);
        if (cost == null) {
            cost = new BigDecimal(0);
        }
        return cost.abs();
    }
}
